package com.hue.gulimail.gulimailthire.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * @author 姚凤桥
 * @Date 2021/11/24 15:25
 * @Description: 发送电子邮件的工具类
 */
@Component
public class EmailUtils {
    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;

    /**
     * 指定收件人、标题、内容，发送一封简单邮件
     *
     * @param target: 收件人
     * @param title:  标题
     * @param text:   内容
     * @return
     */
    public boolean sendEmail(String target, String title, String text) {
        SimpleMailMessage email = new SimpleMailMessage();
        try {
            email.setFrom(from);
            email.setTo(target);
            email.setSubject(title);
            email.setText(text);
            javaMailSender.send(email);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
